package com.zrkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrkj.entity.TSysUserRole;
import com.zrkj.entity.vo.Result;
import com.zrkj.mapper.TSysUserRoleMapper;
import com.zrkj.service.ITSysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : 国良
 * @className : ITSysUserRoleServiceImpl
 * @package: com.ha.service.impl
 * @description : 用户关联角色表 服务接口实现
 * @date :  2021-05-14
 */
@Service("iTSysUserRoleService")
public class ITSysUserRoleServiceImpl extends ServiceImpl<TSysUserRoleMapper, TSysUserRole> implements ITSysUserRoleService {
    /**
     * 添加
     *
     * @param tSysUserRole
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(TSysUserRole tSysUserRole) {
        return Result.success(this.save(tSysUserRole));
    }

    /**
     * 更新
     *
     * @param tSysUserRole
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> update(TSysUserRole tSysUserRole) {
        return Result.success(this.updateById(tSysUserRole));
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> delete(List<Long> idList) {
        return Result.success(this.removeByIds(idList));
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<TSysUserRole> getById(Long id) {
        return Result.success(baseMapper.selectById(id));
    }

    /**
     * 查询列表
     *
     * @param tSysUserRole
     * @return
     */
    @Override
    public Result<List<TSysUserRole>> queryList(TSysUserRole tSysUserRole) {
        QueryWrapper<TSysUserRole> queryWrapper = buildQuery(tSysUserRole);
        return Result.success(this.list(queryWrapper));
    }


    private QueryWrapper<TSysUserRole> buildQuery(TSysUserRole tSysUserRole){
        QueryWrapper<TSysUserRole> queryWrapper = new QueryWrapper<TSysUserRole>();
        queryWrapper.eq(StringUtils.isNotBlank(tSysUserRole.getUserId()),"user_id",tSysUserRole.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(tSysUserRole.getRoleId()),"role_id",tSysUserRole.getRoleId());
        return queryWrapper;
    }
}
